package com.dunqi.gpm.shunqing.home.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.FragmentCallBack;
import com.dunqi.gpm.shunqing.common.util.LogUtils;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.home.fragment.ProjectListFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;

public class ProjectListActivity extends BaseAct implements FragmentCallBack {
    @Override
    public void callBackkUri(Uri uri) {
//        LogUtils.w("Uri", uri.toString());
//        LogUtils.w("Uri", uri.getHost());
//        LogUtils.w("Uri", uri.getScheme());
//        LogUtils.w("Uri", uri.getPath());
        if ("refresh".equals(uri.getScheme())) {
            titleBar.setRightText(uri.getHost());
        }
//        if(uri.ge){}
    }

    private static final String DATA = "data";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;

    private HashMap<String, String> params;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        if (getIntent() != null) {
            params = (HashMap<String, String>) getIntent().getSerializableExtra(DATA);
        }
        replace();
//        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), ProjectInquiryActivity.class);
//                startActivityForResult(intent, 100);
//            }
//        });

        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ProjectInquiryActivity.class);
                PopupMenu pop = new PopupMenu(ProjectListActivity.this, view);//v是加号控件
                pop.getMenuInflater().inflate(R.menu.project_serch_menu, pop.getMenu());
                pop.show();
                pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.action_all:
                                EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("all"));
                                titleBar.setRightText("全部项目");
                                break;
                            case R.id.action_shengzhongdian:
                                EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("省重点"));
                                titleBar.setRightText("省重点");
                                break;
                            case R.id.action_shizhongdian:
                                EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("市重点"));
                                titleBar.setRightText("市重点");
                                break;
                            case R.id.action_quzhongdian:
                                EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("区重点"));
                                titleBar.setRightText("区重点");
                                break;
                            case R.id.action_quzhongdian_tuijin:
                                EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("区重点推进"));
                                titleBar.setRightText("区重点推进");
                                break;

                        }
                        return false;
                    }
                });
            }
        });
    }

    private void replace() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, ProjectListFragment.newInstance(params, 0))
                .commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 400) {
            params = (HashMap<String, String>) data.getSerializableExtra("data");
            replace();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_my_follow;
    }

    public static void start(Context context, HashMap<String, String> params) {
        Intent intent = new Intent(context, ProjectListActivity.class);
        intent.putExtra(DATA, params);
        context.startActivity(intent);
    }


}
